package com.xigua._栈;

import java.util.Stack;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/2613:33
 */
public class _剑指Offer31_栈的压入弹出序列 {

    public static void main(String[] args) {

    }

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int pPoint = 0;
        while (i <pushed.length){
            if (pushed[i]!=popped[pPoint]){
                stack.push(pushed[i]);
            }else {
                pPoint++;
                while (!stack.isEmpty() && stack.peek() == popped[pPoint]){
                    stack.pop();
                    pPoint++;
                }
            }
            i++;
        }
        return stack.isEmpty();
    }

}
